import Dialog from "../../Game/dialog/Dialog";
import Scene from "../../Scene";
import { DirectionCode, SceneState, TButton, TLayout } from "../../SceneControl";

const { ccclass, property } = cc._decorator;

@ccclass
export default class DispatchDialogScene extends Scene {

    protected onEnable(): void {
        super.onEnable();
        this.initScene();
    }

    public initScene() {
        let layout = new TLayout();
        let btns = this.getBtns();
        if (btns && btns.length) {
            layout.buttonList = btns;
        } else {
            let temp = new TButton(this.node);
            temp.addKeyDownEvent((para => {
                if (para.DirectionCode == DirectionCode.ENTER)
                    this.getComponent(Dialog).closeDialog();
            }))
            layout.buttonList = [[temp]];
            this.tscene.sceneState = SceneState.hideFinger;
        }

        this.tscene.addLayout(layout);
        this.tscene.onBack = () => {
            this.getComponent(Dialog).closeDialog();
        }
        this.tscene.joinController();
    }

    refresh() {
        let btns = this.getBtns();
        if (btns) {
            this.tscene.currLayout.buttonList = btns;
        } else {
            this.tscene.currLayout.buttonList = [[new TButton(this.node)]];
            this.tscene.sceneState = SceneState.hideFinger;
        }
        this.tscene.apply();
    }

    getBtns() {
        let buts = cc.find("img_blackBorder/buts", this.node);
        if (!buts.childrenCount) return null;
        let btns = [];
        for (let i = 0, time = 0; i < buts.children.length; i++) {
            if (buts.children[i].active) {
                let line = Math.floor(time / 3);
                let btn = new TButton(buts.children[i]);
                btn.offset = cc.v3(80);
                btns[line] ??= [];
                btns[line].push(btn);
                time++;
            }
        }
        return btns;
    }

}
